package com.qire.manhua.view.animator;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.view.View;
import android.view.View.OnAttachStateChangeListener;
import android.view.animation.DecelerateInterpolator;

import com.qire.common.widget.animation.AnimationBinder;

public class SwingAnimationBinder implements AnimationBinder, AnimatorUpdateListener, AnimatorListener {

    private final ValueAnimator floatAnimator = ValueAnimator.ofFloat(0, 1);
    private View view;
    private int moveScope;
    private int moveDistance = 0;
    private float limitRotation = 6f;

    private static int delayed = 0;

    private final OnAttachStateChangeListener detachedFromWindowListener = new OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View v) {

        }
        @Override
        public void onViewDetachedFromWindow(View v) {
            destroy();
        }
    };

    public SwingAnimationBinder() {
        this(1000, delayed, 10);
//        delayed+=300;
    }

    public SwingAnimationBinder(long duration, int delayed, int moveScope) {
        this.moveScope = moveScope;
        floatAnimator.setDuration(duration);
        floatAnimator.setInterpolator(new DecelerateInterpolator());
        floatAnimator.setRepeatCount(ValueAnimator.INFINITE);//重放次数
        floatAnimator.setRepeatMode(ValueAnimator.RESTART);
        floatAnimator.setStartDelay(delayed);
        floatAnimator.addListener(this);
        floatAnimator.addUpdateListener(this);
    }

    public void pause() {
        if(floatAnimator.isRunning()) {
            floatAnimator.pause();
            moveToTopAndBottom(0, 0);
        }
    }

    public void goPlay(){
        if(!floatAnimator.isStarted())
            floatAnimator.start();
        floatAnimator.resume();
    }

    @Override
    public void bindView(View view) {
        this.view = view;
        floatAnimator.start();
        view.addOnAttachStateChangeListener(detachedFromWindowListener);
    }

    @Override
    public void destroy() {
        floatAnimator.cancel();
        if(view == null) {
            return;
        }
        view.removeOnAttachStateChangeListener(detachedFromWindowListener);
        view = null;
    }

    @Override
    public void onAnimationStart(Animator animation) {}

    @Override
    public void onAnimationEnd(Animator animation) {}

    @Override
    public void onAnimationCancel(Animator animation) {}

    @Override
    public void onAnimationRepeat(Animator animation) {
        floatAnimator.setRepeatMode(ValueAnimator.REVERSE);
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float fraction = animation.getAnimatedFraction();
        int moveToValue = evaluateMove(fraction);
        float rotation  = evaluateRotation(fraction);
        moveToTopAndBottom(moveToValue, rotation);
    }

    private int evaluateMove(float fraction) {
        int moveToValue = (int) (fraction * moveScope);
        return moveToValue;
    }

    private float evaluateRotation(float fraction) {
        float coefficientR = (float) Math.sin(fraction * 20);
        float value = coefficientR * -limitRotation;
        return value;
    }

    private void moveToTopAndBottom(int newTop,float rotation){
        if(view == null)
            return;
//        int offsetDistance = newTop - moveDistance;
//        view.offsetTopAndBottom(-offsetDistance);
//        moveDistance = newTop;

        view.setRotation(rotation);
    }

}
